/*
*编辑私募产品
*EditPrivatePdt.js*/

import React, { PropTypes, PureComponent } from 'react';
import { autobind } from 'core-decorators';
import { Button, Modal, Form, Input, Icon,Select } from 'antd';
import { connect } from 'react-redux';
import {investStrategy} from '../../../utils/dataConfig';
import style from '../privateManager/home.less';

const Option = Select.Option;
const FormItem = Form.Item;
const EditPrivatePdtForm = Form.create()(
	(props) => {
		const { visible, onCancel, onCreate, form,tag,data,} = props;
		console.log('EditPrivatePdtForm',props)
		const { getFieldDecorator } = form;
		
			const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
    };

		return (
			visible ?
				<Modal
					className={` add-EditPrivatePdt`}
					visible={visible}
					title="编辑"
					okText="提交"
					cancelText="取消"
					onCancel={onCancel}
					onOk={onCreate}
				>
					<Form layout="vertical" className={style.EditPrivatePdt}>
						{/*<FormItem label="备案码">*/}
						<FormItem label="备案码" {...formItemLayout}>
              {getFieldDecorator('recordCode', {
                rules: [{ message: '请输入备案码' }],
                initialValue: data.recordCode,
              })(
                <Input />
              )}
            </FormItem>
						{/*<FormItem label="产品名称">*/}
						<FormItem label="产品名称" {...formItemLayout}>
              {getFieldDecorator('productName', {
                rules: [{ required: true, message: '请输入产品名称' }],
                initialValue: data.productName,
              })(
                <Input />
              )}
            </FormItem>
            {/*<FormItem label="产品Code">*/}
						{/*<FormItem label="产品Code" {...formItemLayout}>
              {getFieldDecorator('productCode', {
                rules: [{ required: true,whitespace: true, message: '请输入产品Code' }],
              	initialValue: data.productCode,
              })(
                <Input />
              )}
            </FormItem>*/}
            {/*<FormItem label="投资策略">*/}
						<FormItem label="投资策略" {...formItemLayout}>
              {getFieldDecorator('investStrategy', {
                rules: [{ required: true,whitespace: true, message: '请输入产品Code' }],
              	initialValue: data.investStrategy,
              })(
                <Select>
                	{
                		investStrategy.map((item,i)=><Option value={`${item.value}`} key={i}>{item.name}</Option>)
                	}
                </Select>
              )}
            </FormItem>
            
					</Form>
				</Modal> : null
		);
	},
);
const mapStateToProps = state => ({
	pdtData:state.privatePdt.pdtData,
	managerList:state.privatePdt.managerList
});
const mapDispatchToProps = {
	editPrivatPdt: query => ({
		type: 'privateMger/editPrivatPdt',
		payload: query || {},
	}),
	
};

@connect(mapStateToProps, mapDispatchToProps)
class EditPrivatePdt extends PureComponent {
	static propTypes = {
		form: PropTypes.object.isRequired,
	}
	constructor(props) {
		super(props);
		this.state = {
			visible: false,
			modalHeight: 0,
		};
	}
	componentDidUpdate() {
		const screenHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
		const ele = document.querySelector('.add-EditPrivatePdt .ant-modal-body');
		if (ele) {
			ele.style.height = `${screenHeight * 0.6}px`;
		}
	}

	showModal = () => {
		this.setState({ visible: true });
	}

	handleCancel = () => {
		this.setState({ visible: false });
	}

	handleCreate = () => {
		const form = this.form;
		const {editPrivatPdt,data} = this.props;
		console.log('handleCreate>>',data)
		const productId = data.productId;
		form.validateFields((err, values) => {
			if (err) {
				return;
			}
			const obj ={}
			console.log('Received values of form:>>>', values,data);
			
			// if(data.productCode == values.productCode){
			// 	delete values.productCode
			// }
			investStrategy.map(i=>{
				if(i.name == data.investStrategy){
					data.investStrategy = i.value
				}
			})
			investStrategy.map(i=>{
				if(i.name == values.investStrategy){
					values.investStrategy = i.value
				}
			})
			for(let i in data) {
     		for(let k in values){
     			if(i == k&&data[i] !== values[k]){
     				obj[k]=values[k];
     			}
     				
     		}
			}
			
			console.log('Received',obj)
			editPrivatPdt({
				...obj,
				'productId':productId,
			})
			form.resetFields();
			this.setState({ visible: false });
		});
	}

	saveFormRef = (form) => {
		this.form = form;
	}

	render() {
		const {tag,data,Cname} = this.props;
		return (
			<span >
				<a onClick={this.showModal}>编辑</a>
				<EditPrivatePdtForm
					ref={this.saveFormRef}
					visible={this.state.visible}
					onCancel={this.handleCancel}
					onCreate={this.handleCreate}
					tag={tag}
					data={data}
					Cname={Cname}
				/>
			</span>
		);
	}
}
export default Form.create({
})(EditPrivatePdt);